package procilege.impl.expand;

import procilege.compute.ExprExpandTraveller;
import procilege.compute.PlaceTypeMetadata;
import procilege.compute.PlaceType;
import procilege.lang.PrivExprComposition;
import procilege.lang.PrivExprElement;
import procilege.lang.PrivExprMergence;

@PlaceTypeMetadata(type = PrivExprComposition.class)
public class PrivExprCompositionExpandPlace implements
		PlaceType<ExprExpandTraveller> {

	@SuppressWarnings("unchecked")
	public void visit(ExprExpandTraveller tr, Object place) {

		PrivExprComposition plus = new PrivExprComposition();

		for (PrivExprElement t : (PrivExprComposition) place) {

			tr.setout(t);
			PrivExprElement r = (PrivExprElement) tr.getPreviousVisitResult();
			if (!(r instanceof PrivExprMergence)
					&& !(r instanceof PrivExprComposition)) {

				PrivExprMergence m = new PrivExprMergence(tr.getBoundNames());
				m.mergePrivilege((PrivExprElement) r);

				r = m;
			}
			plus.add(r);
		}

		tr.result(plus);
	}
}